package com.xingfudazi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xingfudazi.app.domain.AppMemberInfo;
import com.xingfudazi.app.domain.AppMoment;
import com.xingfudazi.app.domain.AppMomentComment;
import com.xingfudazi.app.domain.AppMomentLike;
import com.xingfudazi.app.mapper.AppMemberInfoMapper;
import com.xingfudazi.app.mapper.AppMomentLikeMapper;
import com.xingfudazi.app.service.IAppMemberInfoService;
import com.xingfudazi.app.service.IAppMomentLikeService;
import com.xingfudazi.app.service.IAppMomentService;
import com.xingfudazi.common.enums.DBDataStatus;
import com.xingfudazi.common.exception.ServiceException;
import com.xingfudazi.common.utils.DateUtils;
import com.xingfudazi.common.utils.SecurityUtils;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 动态-喜欢Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class AppMomentLikeServiceImpl implements IAppMomentLikeService {
    @Autowired
    private AppMomentLikeMapper appMomentLikeMapper;

    @Autowired
    private IAppMemberInfoService appMemberInfoService;

    @Autowired
    private IAppMomentService appMomentService;


    /**
     * 查询动态-喜欢
     *
     * @param id 动态-喜欢主键
     * @return 动态-喜欢
     */
    @Override
    public AppMomentLike selectAppMomentLikeById(Long id) {
        return appMomentLikeMapper.selectById(id);
    }

    /**
     * 查询动态-喜欢列表
     *
     * @param appMomentLike 动态-喜欢
     * @return 动态-喜欢
     */
    @Override
    public List<AppMomentLike> selectAppMomentLikeList(AppMomentLike appMomentLike) {
        LambdaQueryWrapper<AppMomentLike> wrapper = new LambdaQueryWrapper<>();
        Long mid = appMomentLike.getMid();
        wrapper.eq(AppMomentLike::getMid, mid);
        wrapper.orderByAsc(AppMomentLike::getCreateTime);
        return appMomentLikeMapper.selectList(wrapper);
    }

    /**
     * 新增动态-喜欢
     *
     * @param appMomentLike 动态-喜欢
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertAppMomentLike(AppMomentLike appMomentLike) {
        Long mid = appMomentLike.getMid();
        Long nid = SecurityUtils.getUserId();

        LambdaQueryWrapper<AppMomentLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppMomentLike::getMid, mid);
        wrapper.eq(AppMomentLike::getNid, nid);
        if (this.appMomentLikeMapper.selectCount(wrapper) > 0) {
            throw new ServiceException("喜欢一次了、别太喜欢了");
        }

        AppMemberInfo appMemberInfo = this.appMemberInfoService.selectAppMemberInfoById(nid);
        appMomentLike.setNid(nid);
        appMomentLike.setNickName(appMemberInfo.getNickName());
        appMomentLike.setAvatar(appMemberInfo.getAvatar());
        appMomentLike.setCreateTime(LocalDateTime.now());
        appMomentLike.setId(null);
        appMomentLikeMapper.insert(appMomentLike);

        if (mid != null) {
            this.appMomentService.like(mid, 1);
        }

        return 1;
    }

    /**
     * 修改动态-喜欢
     *
     * @param appMomentLike 动态-喜欢
     * @return 结果
     */
    @Override
    public int updateAppMomentLike(AppMomentLike appMomentLike) {
        return appMomentLikeMapper.updateById(appMomentLike);
    }

    /**
     * 删除动态-喜欢信息
     *
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteAppMomentLikeById(Long mid) {
        Long nid = SecurityUtils.getUserId();
        LambdaQueryWrapper<AppMomentLike> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppMomentLike::getMid, mid);
        wrapper.eq(AppMomentLike::getNid, nid);
        AppMomentLike appMomentLike = appMomentLikeMapper.selectOne(wrapper);
        if (appMomentLike == null) {
            throw new ServiceException("您没有喜欢");
        }
        this.appMomentService.like(mid, -1);
        return appMomentLikeMapper.deleteById(appMomentLike.getId());
    }
}
